package ihm;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import database.ConnexionDB;
import data.*;

import java.awt.Button;
import java.io.File;
import javax.swing.JCheckBox;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComboBox;
public class FenetreEnvoyerIndice extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private ModeleDynamiqueObjetIndice modele;
	private String Type_fichier;
	
	private ConnexionDB DB;
	
	//private ModeleDynamiqueObjetIndice  modele = new ModeleDynamiqueObjetIndice ();
    private JTable tableau;
    private JTextField txtDsq;
    private JTextField textField_2;
   // private final Action action = new SwingAction();



	/**
	 * Create the frame.
	 */
	public FenetreEnvoyerIndice(ModeleDynamiqueObjetIndice mdoui, final Authentification auth) throws SQLException {
		setTitle("Murder Party - Envoyer un indice");		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			}
		});
		
		
		int i;
	
		modele = mdoui;
		ArrayList<Utilisateur> listUsers = new ArrayList<Utilisateur>();
		listUsers = getUserList();
		
		ArrayList<Indice> listIndice = new ArrayList<Indice>();
		listIndice = getListIndice();
		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 292, 367);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		

		


		JLabel lblEnvoyeruNIndice = new JLabel("Envoyer un indice");
		lblEnvoyeruNIndice.setFont(new Font("Comic Sans MS", Font.PLAIN, 16));
		lblEnvoyeruNIndice.setBounds(68, 23, 142, 14);
		contentPane.add(lblEnvoyeruNIndice);
	
		
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setBounds(24, 160, 142, 20);
		contentPane.add(comboBox);
		
		
		
		JLabel lblNomDuJoueur = new JLabel("Nom de l'indice");
		lblNomDuJoueur.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		lblNomDuJoueur.setBounds(24, 65, 105, 14);
		contentPane.add(lblNomDuJoueur);
		
		JLabel lblNomDeLindice = new JLabel("Nom du joueur");
		lblNomDeLindice.setFont(new Font("Comic Sans MS", Font.PLAIN, 13));
		lblNomDeLindice.setBounds(24, 135, 105, 14);
		contentPane.add(lblNomDeLindice);
		
		final JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setBounds(24, 90, 142, 20);
		contentPane.add(comboBox_1);
		
		
	
		
		JButton btnValider = new JButton("Valider");
		btnValider.setBounds(43, 279, 89, 23);
		contentPane.add(btnValider);
		btnValider.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
				
				int id_indice = modele.getIdIndice((String)comboBox_1.getSelectedItem());
		
				int id_joueur = modele.getIdJoueur((String)comboBox.getSelectedItem());
				
				EnvoyerIndice(id_indice,id_joueur);
				
				// On met a jour le client
				auth.setUtilisateur(auth.getUtilisateur());
				
				FermerFenetre();
		}

		
			

			
			});
			
		
		JButton btnAnnuler = new JButton("Annuler");
		btnAnnuler.setBounds(160, 279, 89, 23);
		contentPane.add(btnAnnuler);
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {	
				
				FermerFenetre();
			
		}

			

			
			});
			
		
		
		
		for ( i = 0 ; i < listUsers.size();i++){
			
		int id_utilisateur = listUsers.get(i).getIdUtilisateur();
		comboBox.addItem(listUsers.get(i).getPseudoUtilisateur());

			
		}
		
		
		
	
		for ( i = 0 ; i < listIndice.size();i++){
		
				
			comboBox_1.addItem(listIndice.get(i).getLabelIndice());
				
			}
		
		
		
		
	}
	
	
	private ArrayList<Indice> getListIndice() {
		// Auto-generated method stub
		return modele.getIndiceList();
	}




	private  ArrayList<Utilisateur> getUserList() {
		
		return modele.getUserList();
		
	}


	private  ArrayList<Utilisateur> getJoueurIndice(int id_indice) {
		
		return modele.getJoueurIndice(id_indice);
		
	}
	
	private void FermerFenetre() {
	this.dispose();
		
	}
	private void EnvoyerIndice(int id_indice, int id_joueur) {
	
		modele.envoyerIndice(id_indice,id_joueur);
		
	}

	
	
}
